package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ex3 extends JFrame {

	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JTextField su1;
	private JLabel lblNewLabel_1;
	private JTextField su2;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTextArea result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ex3 frame = new ex3();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ex3() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNewLabel());
		contentPane.add(getSu1());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getSu2());
		contentPane.add(getBtnNewButton());
		contentPane.add(getScrollPane());
	}

	public void prime() {
		int min = 0, max = 0;
		boolean b = true; // 솟수인 경우
		int cnt = 0;
		long sttime = 0;
		long etime = 0;

		try {
			int s1 = Integer.parseInt(su1.getText());
			int s2 = Integer.parseInt(su2.getText());

			min = (s1 < s2) ? s1 : s2;
			max = (s1 > s2) ? s1 : s2;

			result.setText("");
			sttime = System.currentTimeMillis();

			for (int k = 0; k <= max; k++) {
				int q = (int) Math.sqrt(k);
				b = true;

				// 2로 나눔
				if (k % 2 == 0) {
					b = false;
				} else {
					for (int i = 2; i <= q; i = i + 2) {
						if (k % i == 0) {
							b = false;
							break;
						}

					}
				}
				if (b) {
					cnt++;
					result.append(k + " ");
				}

			}
			etime = System.currentTimeMillis();
			result.append("\n소수 개수:" + cnt);
			result.append("\n경과시간:" + (etime - sttime) / 1000 + "초");

		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "정수형만 입력하세요.... 제발!!");
		}
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("범위");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setBounds(25, 25, 57, 15);
		}
		return lblNewLabel;
	}

	private JTextField getSu1() {
		if (su1 == null) {
			su1 = new JTextField();
			su1.setBounds(94, 22, 57, 21);
			su1.setColumns(10);
		}
		return su1;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("~");
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_1.setBounds(163, 25, 27, 15);
		}
		return lblNewLabel_1;
	}

	private JTextField getSu2() {
		if (su2 == null) {
			su2 = new JTextField();
			su2.setBounds(202, 22, 57, 21);
			su2.setColumns(10);
		}
		return su2;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					 double start = System.currentTimeMillis();
//					
//					 int x = Integer.parseInt(su1.getText());
//					 int y = Integer.parseInt(su2.getText());
//					
//					 if (x > y) {
//					 int temp = x;
//					 x = y;
//					 y = temp;
//					 }
//					
//					 if (x <= y) {
//					 for (int i = x; i <= y; i++) {
//					 if (i % 2 == 0) {
//					 if (i == 2) {
//					 result.append("\n" + i);
//					 }
//					 } else if (i % 3 == 0) {
//					 if (i == 3) {
//					 result.append("\n" + i);
//					 }
//					 } else if (i % 5 == 0) {
//					 if (i == 5) {
//					 result.append("\n" + i);
//					 }
//					 } else if (i % 7 == 0) {
//					 if (i == 7) {
//					 result.append("\n" + i);
//					 }
//					 } else {
//					 result.append("\n" + i);
//					 }
//					
//					 }
//					 result.append("\n");
//					 double end = System.currentTimeMillis();
//					 double time = end - start;
//					 result.append("경과 시간 : " + ((double) time / 1000)
//					 + "초\n");
//					 }
					prime();
				}

			});
			btnNewButton.setBounds(288, 21, 97, 23);
		}
		return btnNewButton;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(29, 65, 356, 72);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}

	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
}
